package com.pro.xwt.manager.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.model.BProject;
import com.pro.xwt.common.service.BEmployeeService;
import com.pro.xwt.common.service.BProjectService;
import com.pro.xwt.common.service.BTaskService;
import com.pro.xwt.common.service.RProjEmpService;
import com.pro.xwt.common.vo.BEmployeeVO;
import com.pro.xwt.common.vo.BProjectVO;

@Controller
@RequestMapping("/gathers")
public class GatherController {
	@Autowired
	BProjectService brojectService;
	@Autowired
	BTaskService bTaskService;
	@Autowired
	BEmployeeService bEmployeeService;
	@Autowired
	RProjEmpService rProjEmpService;
	
	@RequestMapping(value = "/lists", method = RequestMethod.GET)
	public String to(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

			return "manager/gather/list";	
	}
	
	

	/**
	 * 方块
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	@ResponseBody
	public Map<String, Object> list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//项目
		 Map<String,Object> mapProjects = brojectService.differentStateProjectSum();
	    int finalProSum =Integer.parseInt(String.valueOf(mapProjects.get("SUM(CASE WHEN end_time <NOW() THEN 1 ELSE 0 END)")));
		int projectingSum =Integer.parseInt(String.valueOf(mapProjects.get("SUM(CASE WHEN end_time >NOW() THEN 1 ELSE 0 END)")));
		int projectSum =Integer.parseInt(String.valueOf(mapProjects.get("COUNT(id)")));
		//任务
		Map<String,Object> mapTasks = bTaskService.differentStateTaskSum();
		int taskSum = Integer.parseInt(String.valueOf(mapTasks.get("COUNT(id)")));
		int taskingSum=Integer.parseInt(String.valueOf(mapTasks.get("SUM(CASE WHEN (last_end_time >NOW() AND com_state='0')THEN 1 ELSE 0 END)")));
		int completeTaskSum=Integer.parseInt(String.valueOf( mapTasks.get("SUM(CASE WHEN com_state = '1' THEN 1 ELSE 0 END)")));
		int overTimeTaskSum =Integer.parseInt(String.valueOf(mapTasks.get("SUM(CASE WHEN (last_end_time <NOW() AND com_state='0') THEN 1 ELSE 0 END)")));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deleteState", "0");
		int employeeSum = bEmployeeService.count(map);
		// 活跃人数
		int activeSum = bTaskService.activeEmployee();
		map.clear();
		map.put("projectSum", projectSum);
		map.put("finalProSum", finalProSum);
		map.put("projectingSum", projectingSum);
		map.put("taskSum", taskSum);
		map.put("taskingSum", taskingSum);
		map.put("completeTaskSum", completeTaskSum);
		map.put("overTimeTaskSum", overTimeTaskSum);
		map.put("employeeSum", employeeSum);
		map.put("activeSum", activeSum);
		return map;
	}

	/**
	 * 项目统计
	 * @param model
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/projectList", method = { RequestMethod.GET})
	@ResponseBody
	public Object projList(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		List<Map<String, Object>> projCount = rProjEmpService.selectEmpCountAndProj(map);
		List<BProjectVO> list = new ArrayList<BProjectVO>();
		map.put("deleteState","0");
		List<BProject>  bProjects = brojectService.queryListAll(map);
		for (int i = 0; i < projCount.size(); i++) {
			for(BProject bProject :bProjects){
				BProjectVO bProjectVO = new BProjectVO();
				Map<String, Object> resultMap = (Map<String, Object>) projCount.get(i);
			    if(resultMap.get("proj_id").equals(bProject.getId())){
				bProjectVO.setProjId((String) resultMap.get("proj_id"));
				bProjectVO.setEmpCount(Integer.parseInt(String.valueOf((Long) resultMap.get("COUNT(proj_id)"))));
				bProjectVO.setProjName((String) resultMap.get("name"));
				list.add(bProjectVO);
			}
		  }
		}
		map.clear();
		map.put("deleteState", "0");
		map.put("cycleTime", "NONE");
		List<BProjectVO> taskList = bTaskService.selectTaskCountAndProj(map);
		List<BProjectVO> tlist = new ArrayList<BProjectVO>();
		for (int i = 0; i < list.size(); i++) {
			for (int j = 0; j < taskList.size(); j++) {
				if (list.get(i).getProjId().equals(taskList.get(j).getProjId())) {
					list.get(i).setTaskedCount(taskList.get(j).getTaskedCount());
					list.get(i).setTaskSum(taskList.get(j).getTaskSum());
				}	
			}
			
			tlist.add(list.get(i));
		}

		return tlist;
	}
	
    /**
     * 成员统计
     * @param model
     * @param session
     * @param request
     * @param response
     * @return
     */
	@RequestMapping(value = "/empList", method = { RequestMethod.GET})
	@ResponseBody
	public Object empList(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		map.put("deleteState", "0");
		map.put("cycleTime", "NONE");
		List<BEmployeeVO> lists = new ArrayList<>();
		List<BEmployee> bEmployee = bEmployeeService.queryListAll(map);
		List<BEmployeeVO> BEmployeeVOs = bTaskService.selectEmpTasks(map);
		for(int i = 0 ; i<bEmployee.size();i++){
			BEmployeeVO bEmployeeVO = new BEmployeeVO();
			bEmployeeVO.setEmpId(bEmployee.get(i).getId());
			bEmployeeVO.setEmpName(bEmployee.get(i).getName());
			for(int j=0;j<BEmployeeVOs.size();j++){
				if(bEmployee.get(i).getId().equals(BEmployeeVOs.get(j).getEmpId())){
					bEmployeeVO.setTaskSum(BEmployeeVOs.get(j).getTaskSum());
					bEmployeeVO.setTaskedSum(BEmployeeVOs.get(j).getTaskedSum());
					bEmployeeVO.setOverTimeTask(BEmployeeVOs.get(j).getOverTimeTask());
				}	
			}
			lists.add(bEmployeeVO);			
		}		
		return lists;
	}
}
